﻿using HPIT.RentHouse.Common;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entityes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RentHouse.Service
{
    public class RolesService : IRolesService
    {
        #region GetList
        public List<PermissionDTO> GetList()
        {
            RentHouseEntity db = new RentHouseEntity();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            List<PermissionDTO> list = baseService.GetList(e => true).Select(e => new PermissionDTO()
            {
                Id = e.Id,
                Name = e.Name,
                Description = e.Description
            }).ToList();
            return list;
        }
        #endregion

        #region 角色列表
        public List<RoleDTO> GetPagedList(int start, int length, string name, ref int rowcount)
        {
            using (var db = new RentHouseEntity())
            {
                var bs = new BaseService<T_Roles>(db);
                var query = PredicateExtensions.True<T_Roles>();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    query = query.And(a => a.Name.Contains(name));
                }
                var list = bs.GetPagedList(start, length, ref rowcount, query, e => e.Id, false).Select(e => new RoleDTO
                {
                    Id = e.Id,
                    Name = e.Name,
                }).ToList();
                return list;
            }
        }
        #endregion

        #region 添加
        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="editdto"></param>
        /// <returns></returns>
        public AjaxResult AddRole(RoleEditDTO editDTO)
        {
            //先添加角色
            RentHouseEntity db = new RentHouseEntity();
            //BaseService<T_Roles> baseService = new BaseService<T_Roles>(db);
            var bs = new BaseService<T_Roles>(db);
            var baseservice = new BaseService<T_Permissions>(db);

            T_Roles roles = new T_Roles();
            {
                roles.Name = editDTO.Name;
                roles.CreateDateTime = DateTime.Now;
            };
            bs.Add(roles);
            for (int i = 0; i < editDTO.PermissionIds.Count; i++)
            {
                long permissionIds = editDTO.PermissionIds[i];
                var permission = baseservice.Get(e => e.Id == permissionIds);
                if (permission != null)
                {
                    roles.T_Permissions.Add(permission);
                }
                baseservice.Update(permission);
            }
            return new AjaxResult(ResultState.Success, "角色添加成功！");
            //if (id > 0)
            //{
            //    BaseService<T_Permissions> perService = new BaseService<T_Permissions>(db);
            //    //添加角色成功，添加角色权限关联表
            //    foreach (long perid in editDTO.PermissionIds)
            //    {
            //        T_Permissions permissions = perService.Get(e => e.Id == perid);
            //        roles.T_Permissions.Add(permissions);
            //    }
            //    baseService.Update(roles);
            //return new AjaxResult(ResultState.Success, "角色添加成功！");
            //}
            //else
            //{
            //    return new AjaxResult(ResultState.Error, "角色添加失败！");
            //}
        }
        #endregion

        #region 修改
        public RoleEditDTO GetEditRole(long id)
        {
            RentHouseEntity db = new RentHouseEntity();
            var baseService = new BaseService<T_Roles>(db);
            var model = baseService.Get(e => e.Id == id);
            if (model != null)
            {
                var roleEditDTO = new RoleEditDTO()
                {
                    Id = model.Id,
                    Name = model.Name,
                    PermissionIds = model.T_Permissions.Select(e => e.Id).ToList()
                };
                return roleEditDTO;
            }
            return null;
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public AjaxResult Update(RoleEditDTO dto)
        {
            RentHouseEntity db = new RentHouseEntity();
            BaseService<T_Roles> baseService = new BaseService<T_Roles>(db);

            var model = baseService.Get(a => a.Id == dto.Id);
            //更新角色信息
            model.Name = dto.Name;
            //删除原有的权限
            model.T_Permissions.Clear();
            //添加修改后的权限
            BaseService<T_Permissions> perService = new BaseService<T_Permissions>(db);
            foreach (long perid in dto.PermissionIds)
            {
                T_Permissions permissions = perService.Get(a => a.Id == perid);
                model.T_Permissions.Add(permissions);
            }
            if (baseService.Update(model))
            {
                return new AjaxResult(ResultState.Success, "角色修改成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "角色修改失败！");
            }
        }
        #endregion

        #region 单个删除
        public AjaxResult Delete(long id)
        {
            using(var db=new RentHouseEntity())
            {
                var bs = new BaseService<T_Roles>(db);
                var role = bs.Get(e => e.Id == id);
                if (role != null)
                {
                    //1.删除角色对应的权限
                    role.T_Permissions.Clear();
                    //2.删除角色
                    bs.Delete(role);
                    return new AjaxResult(ResultState.Success, "删除成功");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "该角色已经被删除");
                }
            }
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public AjaxResult DeleteBatch(List<long> ids)
        {
            RentHouseEntity db = new RentHouseEntity();
            BaseService<T_Roles> baseService = new BaseService<T_Roles>(db);
            try
            {
                foreach (long Id in ids)
                {
                    T_Roles roles = baseService.Get(e => e.Id == Id);
                    bool flag = baseService.Delete(roles);
                }
                return new AjaxResult(ResultState.Success, "删除成功");
            }
            catch (Exception ex)
            {
                return new AjaxResult(ResultState.Error, "删除失败");
            }
        }
        #endregion
    }
}

